import { createRouter, createWebHistory } from "vue-router"; // 1.导入路由
import Layout from "../views/Layout.vue"; // 导入组件方式1：先导入，下面引用

// 2.定义路由对象
const routes = [
  {
    path: "/",  // URL
    name: "首页",
    component: Layout, // 引用组件
    redirect: '/dashboard',  // 访问首页跳转到仪表盘
    children: [
      {
        path: '/dashboard',
        name: "仪表盘",
        icon: "HomeFilled",
        component: () => import('../views/Dashboard')
      }
    ]
  },

  {
    path: "/nav1",
    name: "一级菜单1",
    icon: "Guide",
    // 导入组件方式2：当路由被访问时才会加载组件（惰性）
    component: Layout,
    children: [
      {
        path: '/nav1/a',
        name: "页面A",
        component: () => import('../views/A')
      },
      {
        path: '/nav1/b',
        name: "页面B",
        component: () => import('../views/B')
      },
    ]
  },
  {
    path: "/nav2",
    name: "一级菜单2",
    icon: "Histogram",
    component: Layout,
    children: [
      {
        path: '/nav1/c',
        name: "页面C",
        component: () => import('../views/C')
      }
    ]
  }
];

// 3.创建路由实例并传递上面定义的路由对象
const router = createRouter({
  history: createWebHistory(process.env.BASE_URL), // 路由模式
  routes,
});

// router.beforeEach((to,from,next) => {
//   console.log(to.path,from.path,next);
//   // 如果用户访问登录页，直接放行
//   if(to.path == "/login") {
//     return next();
//   }
//   const token = "666"; // 模拟token，正常是从本地session存储获取
//   if(token) {
//     next()  // 正常跳转
//   } else {
//     return next('/login') // 跳转转到登录页
//   }
// });

export default router;
